package com.zyg.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson2.JSON;
import com.zyg.pojo.EchartsData;
import com.zyg.pojo.Permission;
import com.zyg.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class IndexController {

    @Autowired
    private UserService userService;

    //    @SaCheckLogin
    @RequestMapping("/toindex")
    public String index(Model model){
        Integer loginId = StpUtil.getLoginIdAsInt();
        List<Permission> menuList = new ArrayList<>();
        if (loginId != null && loginId == 1) {
            // 查询全部菜单
            menuList = userService.getMenuList();
        } else {
            // 根据用户id 查询菜单
            menuList = userService.getMenuListByUserId(loginId);
        }
        model.addAttribute("menuList",menuList);
        return "index";
    }


    //    @SaCheckLogin
    @RequestMapping("/index_v1")
    public String index_v1(){
        return "index_v1";
    }

    @RequestMapping("/getData")
    @ResponseBody
    public String getData(){
        List<EchartsData> data =  userService.getDeptIndexData();
        Map<String, Object> map = new HashMap<>();
        if (data != null && data.size() > 0){
            map.put("name",data.stream().map(e -> e.getName()).toList());
            map.put("value",data.stream().map(e -> e.getValue()).toList());
        }
        return JSON.toJSONString(map);
    }
    @RequestMapping("/getData2")
    @ResponseBody
    public String getData2(){
        List<EchartsData> data2 = userService.getClassIndexData();
        Map<String, Object> map = new HashMap<>();
        if (data2 != null && data2.size() > 0){
            map.put("name",data2.stream().map(e -> e.getName()).toList());
            map.put("value",data2.stream().map(e -> e.getValue()).toList());
        }
        return JSON.toJSONString(map);
    }
    @RequestMapping("/getData3")
    @ResponseBody
    public String getData3(){
        List<EchartsData> data3 = userService.getGradeIndexData();
        Map<String, Object> map = new HashMap<>();
        if (data3 != null && data3.size() > 0){
            map.put("name",data3.stream().map(e -> e.getName()).toList());
            map.put("value",data3.stream().map(e -> e.getValue()).toList());
        }
        return JSON.toJSONString(map);
    }

}
